<%@ page import="org.gnomus.server.Context" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="org.gnomus.server.C" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.google.gson.Gson" %>
<%
  Context ctx = Context.get();
  List<Map> lessons = (List<Map>) ctx.data.get("lessons");
  Map<String, List> lessonsBySubject = new HashMap<String, List>();
  if (lessons != null) {
    for (Map lesson : lessons) {
      List<Map> lesson_list = lessonsBySubject.get(lesson.get("subject"));
      if (lesson_list == null) { lesson_list = new ArrayList<Map>(); }
      lesson_list.add(lesson);
      lessonsBySubject.put((String) lesson.get("subject"), lesson_list);
    }
  }
%>
<script>
  var lessons = JSON.parse('<%= new Gson().toJson(lessons) %>');
</script>
<div style="width:640px; margin:0 auto;">
  <h3>Lessons</h3>
  <div id="lessons">
  <%
    if (lessons == null || lessons.size() == 0) {
  %>
  <p>
    No lessons found.
  </p>
  <%
    } else {
      for (Map.Entry<String, List> me : lessonsBySubject.entrySet()) {
        String subject = C.LESSON_TOPICS.get(me.getKey());
        List<Map> lesson_list = me.getValue();
  %>
  <h3><%= subject %></h3>
  <table style="width:100%;">
  <%
        for (Map lesson : lesson_list) {
          String title = (String) lesson.get("title");
          String href = "/learn/lesson?id=" + lesson.get("id");
  %>
  <tr>
    <td style="width:80%">
      <a href='<%= href %>'><%= title %></a>
    </td>
  </tr>
  <%
        }
  %>
  </table>
  <%
      }
    }
  %>
  </div>
</div>
<div class="clear"></div>
